<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : SearchCategory.xsl
    Created on : February 19, 2012, 4:44 PM
    Author     : Truong
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="search"/>
    <xsl:param name="category"/>
    <xsl:param name="priceFrom"/>
    <xsl:param name="priceTo"/>

    <xsl:output method="html"/>

    <!-- TODO customize transformation rules 
         syntax recommendation http://www.w3.org/TR/xslt 
    -->
    <xsl:template match="/">
        <html>
            <body>
                <xsl:call-template name="searchCategory"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template name="searchCategory">
        <xsl:value-of select="$search"/>
        <table>
            <tr>
                <xsl:for-each select="root/product_list/product[category = $category and price &gt;= $priceFrom and price &lt;= $priceTo]">
                    <xsl:param name="position" select="(position() - 1)"/>
                    <xsl:param name="mod" select="(position() mod 3)"/>
                    <xsl:param name="counter" select="(position() + 3)"/>
                    <xsl:param name="productCode">
                        <xsl:value-of select="id"/>
                    </xsl:param>
                    <xsl:param name="productForm">
                        Form
                        <xsl:value-of select="id"/>
                    </xsl:param>
                    <xsl:choose>
                        <xsl:when test="$mod = 1 or $position = 0">
                            <table style="border-width: 0px; border-style: solid;">
                                <tr class="blank">
                                    <xsl:call-template name="addTableSearchCategory">
                                        <xsl:with-param name="position" select="$position"/>
                                        <xsl:with-param name="counter" select="$counter"/>
                                        <xsl:with-param name="productForm" select="$productForm"/>
                                        <xsl:with-param name="productCode" select="$productCode"/>
                                    </xsl:call-template>
                                </tr>
                            </table>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </tr>
        </table>
    </xsl:template>

    <xsl:template name="addTableSearchCategory">
        <xsl:param name="position"/>
        <xsl:param name="counter"/>
        <xsl:param name="productForm"/>
        <xsl:param name="productCode"/>

        <xsl:for-each select="//product_list/product[category = $category and price &gt;= $priceFrom and price &lt;= $priceTo] ">
            <xsl:choose>
                <xsl:when test="(position() &gt; $position and position() &lt; $counter)">
                    <td class="blank">
                        <table id="bodyTable" style="border-width: 1px; border-style: solid;" width="200px">
                            <xsl:param name="productName">
                                <xsl:value-of select="name"/>
                            </xsl:param>
                            <xsl:param name="image">
                                <xsl:value-of select="image"/>
                            </xsl:param>
                            <tr>
                                <th class="product" colspan="2" align="center">
                                    <xsl:value-of select="$productName"/>
                                </th>
                            </tr>
                            <tr>
                                <td class="product" colspan="2" align="center">
                                    <center>
                                        <img src="{$image}" title="{$productName}" alt="{$productName}" />
                                    </center>
                                </td>
                            </tr>
                            <tr>
                                <td class="product">
                                    Description:
                                </td>
                                <td class="product">
                                    <xsl:value-of select="detail"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="product">
                                    Price:
                                </td>
                                <td class="product">
                                    $
                                    <xsl:value-of select="price"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="product" align="center" colspan="2">
                                    <input id="buyButton" type="submit" value="Add to cart!"></input>
                                </td>
                            </tr>
                        </table>
                    </td>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
